/******************************** Frame Auto Resize **************************/
/**
To reuse this JS 
1. In FormInitDone() set the values of frh and hrh variables
eg:
	hrh = 185; //header height, write it to get it from index of element id header_app_Anonymous-0 plus secondary_app_Anonymous-0 if available
	frh = 80; //footer height, write it to get it from index of element of id footer_app_Anonymous-0
*/
//global variables for dimensions and DOM 
var w = window;
var d = document;
var e = d.documentElement;
var g = d.getElementsByTagName('body')[0];
var hrh, frh, vph, hrw, frw, vpw, normalHeight;


function frameAutoSize()
{

	vph = w.innerHeight || e.clientHeight || g.clientHeight; //the viewport height
	normalHeight = vph - (frh + hrh);
	//alert('Viewport is: ' + vph + ', normal is: ' + normalHeight);
	var displayFrameElem = d.getElementById('display_frame'); //this is the target frame
	var displayRegionElem = d.getElementById('display_region'); //this is parent.parent.parent of the target frame
	var newHeight = vph;
	var newElemHeight = normalHeight - 100 + 'px';
	var newRegionSetHeight = normalHeight + 'px';

	//determining the maincontainer of the inner document of the target frame
	if (displayFrameElem.contentWindow.document.body)
	{
		xMainContainer = displayFrameElem.contentWindow.document.body.firstChild;

		while (xMainContainer && xMainContainer.className.indexOf('maincontainer') == -1)
		{
			xMainContainer = xMainContainer.nextSibling;
		}
		xMainContainer.style.bottom = 'auto';
	}
	else return false;

	//calculating
	var docHeight = xMainContainer.scrollHeight;
	var bHeight = displayFrameElem.offsetHeight;
	//alert('Calculated body: ' + bHeight + ', Frame height: ' + docHeight );
	//resetting
	g.style.height = vph + 'px';
	//alert('normal body height: ' + g.style.height + ' and the vph is: ' + vph); 
	g.firstChild.style.height = vph + 'px';
	//displayFrameElem.style.height = normalHeight + 'px'; not needed when frame is anchored
	//displayRegionElem.style.height = normalHeight + 'px'; //this is the region
	displayRegionElem.parentNode.style.height = normalHeight + frh + 'px'; //this is the regionset, sibling of header region,parent of display and footer
	header_region.setOffset(hrh + 'px');


	//incrementing 
	if (docHeight > normalHeight)
	{
		newHeight = xMainContainer.scrollHeight + hrh + frh + 170 + 'px';
		newElemHeight = xMainContainer.scrollHeight + 20 + 'px';
		newRegionSetHeight = xMainContainer.scrollHeight + frh + 170 + 'px';
		//alert('Frame height is more than the body height, common scrollbar on root expected');
	}
	else
	{
		newHeight = vph + 'px';
		newElemHeight = (vph - (frh + hrh)) + 'px';
		newRegionSetHeight = (vph - (frh + hrh)) + 'px';
		xMainContainer.style.bottom = '0px';
		//alert('Body can accomodate the frame, reset parents to viewport');
	}
	//alert('NewHeight for top level: ' + newHeight + ', New X height for elements: ' + newElemHeight );
	xMainContainer.style.overflow = 'visible'; //This is to make Free Layout elements to be shown
	g.style.height = newHeight;
	g.firstChild.style.height = newHeight; // first child of body, ie maincontainer
	//displayFrameElem.style.height = newElemHeight;  no need to set this when frame is free layout and anchored
	displayRegionElem.parentNode.style.height = newRegionSetHeight; //the regionset, sibling of header region
	//reset footer offset to align the appPalettes to new region construction
	header_region.setOffset(hrh + 'px');
	//alert('Redrawing complete');
}

function sizeCorrection(eventObject)
{
	frameAutoSize();
}